"use strict"


function onLogin()
{
    Refresh();
}


async function Refresh()
{
    //ShowAssets();
    ShowRunes();
    BalanceOf()
}

// let AssetsAll  = null;
// async function ShowAssets()
// {
//     $.ajax({
//         url: Helper.GetServerIP() + "/Assets",
//                   dataType: "text",
//                   type: "post",
//                   data: { address: Helper.addressCur, offset: 0,count: 12 },
//                   success: function (data) {

//                         // // 删除之前的数据
//                         // var pane_list = $(".tokens-list")[0];
//                         // for (var i = pane_list.children.length - 1; i >= 0; i--) {
//                         //     pane_list.children[i].remove();
//                         // }

//                         // RunesAll = JSON.parse(data);

//                         // for(var ii=0;ii<RunesAll.length;ii++)
//                         // {
//                         //     var item = JSON.parse(RunesAll[ii]);
//                         //     search_insert( pane_list, item.Data, item.Number, item.ID, getDateDiff(item.Created), onclickitem);
//                         // }

//                   },
//                   error: function (err) {
//                     ShowToastrError("Error",err.responseText);
//                   }
//       });

// }


let RunesAll  = null;
async function ShowRunes()
{
    $.ajax({
        url: Helper.GetServerIP() + "/RunesAll",
                  dataType: "text",
                  type: "post",
                  data: { address: Helper.addressCur, offset: 0,count: 12 },
                  success: function (data) {

                        // 删除之前的数据
                        var pane_list = $(".runes-list")[0];
                        for (var i = pane_list.children.length - 1; i >= 0; i--) {
                            pane_list.children[i].remove();
                        }

                        RunesAll = JSON.parse(data);

                        for(var ii=0;ii<RunesAll.length;ii++)
                        {
                            var item = JSON.parse(RunesAll[ii]);
                            runes_insert( pane_list, item.Data, item.Number, item.ID, getDateDiff(item.Created), onclickitem);
                        }

                  },
                  error: function (err) {
                    ShowToastrError("Error",err.responseText);
                  }
      });

}


async function bodyNavClick(pane)
{
    var panechild = $("#AssetsTabs .active");
    for (var ii=0;ii<panechild.length;ii++){
      $(panechild[ii]).removeClass("active");
    }

    var panechild = $(".tab-content .active");
    for (var ii=0;ii<panechild.length;ii++){
      $(panechild[ii]).removeClass("show");
      $(panechild[ii]).removeClass("active");
    }

    $("#AssetsTabs #"+pane).addClass("active");

    $(".tab-content #"+pane + "-pane").addClass("show");
    $(".tab-content #"+pane + "-pane").addClass("active");

    curr_page_index = 0;
    // ShowTabPaneGame();

}


let curr_page_index = 0;
function onPageitem(e){
    var index = Number.parseInt($(e).attr("data"));
    if(index!=curr_page_index){
      curr_page_index = index;
      ShowRunes();
    }
}

async function BalanceOf()
{
    $.ajax({
        url: Helper.GetServerIP() + "/BalanceOf",
                  dataType: "text",
                  type: "post",
                  data: { address: Helper.addressCur,tick: project_tick },
                  success: function (data) {

                    $("#BalanceOf").html( new BigNumber(data).toFormat()  );
                    $("#BalanceOf2").html(  Helper.Div(data,"1000") + " mints" );

                  },
                  error: function (err) {
                    ShowToastrError("Error",err.responseText);
                  }
      });

}

async function doTransferNew()
{
  var addressTo = $("#tranToAddress").val();
  var amountTo = Number($("#tranAmount").val());

  let balance = $("#BalanceOf").text().replace(/,/g,'');
  console.log(balance, Number(balance) < amountTo, amountTo)
  if(!balance || Number(balance) < amountTo) {
    ShowToastrError("Error", 'The balance is insufficient');
    return;
  }

  await toSwitchNetwork();

  let network = await Helper.provider.getNetwork();
  if(network.chainId==myNetwork.chainId)
  {
    try{
      let tranTipSend = $(".tranTipSend");
      tranTipSend.text("Sending...");
      tranTipSend.attr("disabled",true);
      const contract = new window._ethers.Contract(Contract_VOOK, Contract_VOOK_Abi, Helper.provider.getSigner());
      var rel1  = await contract.transfer(addressTo, amountTo);
      console.log(rel1);
      ShowToastr("success","transfer send");
    }catch(e){
      console.log(e);
      if(e.data.message.indexOf("insufficient funds for gas")){
        ShowToastrError("transfer error","insufficient funds for gas", e);
      }
    }finally {
      console.log("???")
      closeTranTip();
      let tranTipSend = $(".tranTipSend");
      tranTipSend.text("transfer");
      tranTipSend.attr("disabled",false);
    }
  }
  else {
    ShowToastrError("transfer error","Please switch the wallet network to ", myNetwork.chainName);
  }
}

function closeTranTip() {
  let tranTip = $(".tranTip");
  tranTip.css('display','none');
}

function showTranTip() {
  let tranTip = $(".tranTip");
  tranTip.css('display','flex');
}

(function($) {
    "use strict"



})(jQuery);


